home *** CD-ROM | disk | FTP | other *** search
- #include <LowMem.h>
- #include <TextUtils.h>
- #include <Gestalt.h>
- #include <MacTypes.h>
- #include <Resources.h>
- #include <Traps.h>
- #include <MixedMode.h>
- #include <Processes.h>
- #include <QDOffscreen.h>
- #include <Folders.h>
- #include <CodeFragments.h>
-
- #include "DockStrip.h"
- #include "MoreCFMPatches.h"
-
- InitGrafUPP gSaveInitGraf;
- Get1IndResourceUPP gSaveGet1IndResource;
- SInt16 gCSAppResFile;
-
- Handle gSdevStub;
- Handle gSdevFragStub;
- FSSpec gInitFileSpec;
-
- SDockStripGlobals gGlobals;
-
- pascal void Patched_InitGraf (
- void* ioGlobals);
- pascal void Patched_SetPort (
- GrafPtr inWindow);
- pascal Handle Patched_Get1IndResource (
- ResType type,
- short index);
- pascal OSErr Patched_GetDiskFragment (
- const FSSpec* fileSpec,
- long offset,
- long length,
- Str63 fragName,
- CFragLoadOptions findFlags,
- CFragConnectionID* connID,
- Ptr* mainAddress,
- Str255 errName);
-
-
-
- UniversalProcPtr ApplyTrapPatch (
- UInt16 trap, UniversalProcPtr patchPtr);
- Boolean HaveProcessManager();
- GWorldPtr CreateGWorld (void);
- Handle LoadSdevStub (void);
- Handle LoadSdevFragStub (void);
- Handle PrepareSdevStub (
- short inIndex);
- ProcPtr PrepareSdevFragStub (
- ProcPtr inRealRoutine);
- void RememberStub (
- Handle stub,
- Handle module);
- void RememberFragStub (
- ProcPtr stub,
- ProcPtr module);
-
- void main (void) {
- Handle initCode = nil;
- THz theZone;
- SInt16 myResFile;
- OSErr err;
- SInt16 systemVolume;
- Str31 myPath;
- SInt32 myDirID;
-
- /* Find reference number for the extension file */
- myResFile = CurResFile ();
- err = ResError ();
-
- if (err == noErr) {
- /* Find startup disk volume reference number */
- long systemFolder;
- err = FindFolder (kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &systemVolume, &systemFolder);
- }
-
- if (err == noErr) {
- /* Figure out which file we're in */
- FCBPBRec paramBlock = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-
- paramBlock.ioNamePtr = myPath;
- paramBlock.ioVRefNum = systemVolume;
- paramBlock.ioRefNum = myResFile;
-
- err = PBGetFCBInfoSync (¶mBlock);
- myDirID = paramBlock.ioFCBParID;
- }
-
- if (err == noErr) {
- err = FSMakeFSSpec (systemVolume, myDirID, myPath, &gInitFileSpec);
- }
-
- /* Detach and lock the code resource */
- initCode = Get1Resource ('INIT', 0);
- if (initCode == nil)
- goto failure;
-
- DetachResource (initCode);
- if (ResError () != noErr)
- goto failure;
-
- theZone = GetZone();
- SetZone (SystemZone());
-
- HLockHi (initCode);
-
- /* Install the InitGrafPatch */
- gSaveInitGraf = (InitGrafUPP) ApplyTrapPatch (_InitGraf, NewInitGrafProc (Patched_InitGraf));
-
- failure:;
- }
-
- UniversalProcPtr ApplyTrapPatch (
- UInt16 trap, UniversalProcPtr patchPtr) {
-
- UniversalProcPtr trapPtr;
-
- if (patchPtr == nil)
- return nil;
-
- trapPtr = NGetTrapAddress (trap, (trap & 0x0800) ? ToolTrap : OSTrap);
- NSetTrapAddress (patchPtr, trap, (trap & 0x0800) ? ToolTrap : OSTrap);
- return trapPtr;
- }
-
- pascal void Patched_InitGraf (
- void* ioGlobals) {
-
- ProcessSerialNumber currentProcess = { 0, kCurrentProcess };
- ProcessInfoRec processInfo;
- OSErr err;
-
- processInfo.processInfoLength = sizeof (processInfo);
- processInfo.processAppSpec = nil;
- processInfo.processName = nil;
-
- if (HaveProcessManager ()) {
- err = GetProcessInformation (¤tProcess, &processInfo);
-
- if ((err == noErr) && (processInfo.processSignature == 'sdev')) {
- static Boolean beenHere = false;
- if (!beenHere) {
- GrafPtr savePort;
- gSaveGet1IndResource = ApplyTrapPatch (_Get1IxResource, NewGet1IndResourceProc (Patched_Get1IndResource));
- MorePatchTVector ((TVector*)GetDiskFragment, (TVector*)Patched_GetDiskFragment, dockStripSignature, nil);
- gGlobals.offscreenGWorld = CreateGWorld ();
- gGlobals.updatePort = CreateGWorld ();
- OpenCPort (&gGlobals.ourPort);
- gSdevStub = LoadSdevStub ();
- gSdevFragStub = LoadSdevFragStub ();
- gCSAppResFile = CurResFile ();
- gGlobals.saveSetPort = ApplyTrapPatch (_SetPort, NewSetPortProc (Patched_SetPort));
- NewGestaltValue (dockStripGestalt, (SInt32)&gGlobals);
- beenHere = true;
- }
- }
- }
-
- CallInitGrafProc (gSaveInitGraf, ioGlobals);
- }
-
- pascal void Patched_SetPort (
- GrafPtr inGrafPort) {
-
- CallSetPortProc (gGlobals.saveSetPort, inGrafPort);
- // CallSetPortProc (gGlobals.saveSetPort, gGlobals.offscreenGWorld);
- // CallSetPortProc (gGlobals.saveSetPort, gGlobals.ourPort);
- }
-
- pascal Handle Patched_Get1IndResource (
- ResType inResType,
- short inIndex)
- {
- // DebugStr ("\pGet1IndResource patch");
- if ((inResType == 'sdev') && (CurResFile () != gCSAppResFile))
- return PrepareSdevStub (inIndex);
-
- return CallGet1IndResourceProc (gSaveGet1IndResource, inResType, inIndex);
- }
-
- Boolean HaveProcessManager()
- {
- static Boolean alreadyKnowWeHaveIt = false;
- SInt32 response;
- OSErr error;
-
- if (alreadyKnowWeHaveIt)
- return true;
-
- error = Gestalt (gestaltOSAttr, &response);
-
- if ((error != noErr) || ((response & gestaltLaunchControl) == 0)) {
- return false;
- }
-
- alreadyKnowWeHaveIt = true;
-
- return true;
- }
-
- GWorldPtr CreateGWorld (void)
- {
- GWorldPtr offscreenGWorld;
- OSErr err;
- Rect bounds = {0, 0, 768, 1024};
- GrafPtr savePort;
- RGBColor back = {0xC000, 0xC000, 0xC000};
- RGBColor old;
-
- /* Create the offscreen GWorld */
- err = NewGWorld (&offscreenGWorld, 0, &bounds, nil, nil, useTempMem);
- if (err != noErr) {
- DebugStr ("\pNewGWorld failed");
- return nil;
- }
-
- GetPort (&savePort);
- SetPort ((GrafPtr)offscreenGWorld);
- GetBackColor (&old);
- RGBBackColor (&back);
- EraseRect (&bounds);
- RGBBackColor (&old);
- SetPort (savePort);
-
- return offscreenGWorld;
- }
-
- Handle LoadSdevStub (void)
- {
- Handle sdevStub;
-
- SInt16 resFile = FSpOpenResFile (&gInitFileSpec, fsRdWrPerm);
- if (resFile == kResFileNotOpened) {
- DebugStr ("\pResource file not found?");
- return nil;
- }
-
- sdevStub = Get1Resource ('sdev', 128);
- if (sdevStub == nil) {
- DebugStr ("\pSdev stub not found?");
- return nil;
- }
-
- DetachResource (sdevStub);
- CloseResFile (resFile);
- return sdevStub;
- }
-
- Handle LoadSdevFragStub (void)
- {
- Handle sdevStub;
-
- SInt16 resFile = FSpOpenResFile (&gInitFileSpec, fsRdWrPerm);
- if (resFile == kResFileNotOpened) {
- DebugStr ("\pResource file not found?");
- return nil;
- }
-
- sdevStub = Get1Resource ('sdev', 129);
- if (sdevStub == nil) {
- DebugStr ("\pSdev frag stub not found?");
- return nil;
- }
-
- DetachResource (sdevStub);
- CloseResFile (resFile);
- return sdevStub;
- }
-
- Handle PrepareSdevStub (
- short inIndex)
- {
- Handle newSdevStub;
- OSErr err;
- UInt32 index;
- Handle realResource;
-
- // DebugStr ("\pPrepareSdevStub");
-
- SetResLoad (true);
- realResource = CallGet1IndResourceProc (gSaveGet1IndResource, 'sdev', inIndex);
-
- if (realResource == nil)
- return nil;
-
- DetachResource (realResource);
- HLock (realResource);
-
- newSdevStub = gSdevStub;
- err = HandToHand (&newSdevStub);
- if (err != noErr) {
- DebugStr ("\pOut of memory in PrepareSdevStub?");
- return nil;
- }
-
- HLock (newSdevStub);
-
- /* Slam the pointer into the new stub */
- for (index = 0; index < GetHandleSize (newSdevStub); index += 1) {
- if (*(UInt32*)((*newSdevStub) + index) == 0xDEADBEEFL) {
- BlockMove (&realResource, &((*newSdevStub) [index]), sizeof (UInt32));
- break;
- }
- }
- RememberStub (newSdevStub, realResource);
- return newSdevStub;
- }
-
- ProcPtr PrepareSdevFragStub (
- ProcPtr realMainAddress)
- {
- Handle newSdevStub;
- OSErr err;
- ProcPtr ourAddress;
- CFragConnectionID connectionID;
- Str255 errName;
-
- // DebugStr ("\pPrepareSdevStub");
-
- newSdevStub = gSdevFragStub;
- err = HandToHand (&newSdevStub);
- if (err != noErr) {
- DebugStr ("\pOut of memory in PrepareSdevFragStub?");
- return nil;
- }
-
- HLock (newSdevStub);
-
- err = GetMemFragment (*newSdevStub, GetHandleSize (newSdevStub), "\pSdev stub", kPrivateCFragCopy,
- &connectionID, (Ptr*)&ourAddress, errName);
- if (err != noErr) {
- DebugStr ("\pError in GetMemFragment");
- return nil;
- }
-
- RememberFragStub (ourAddress, realMainAddress);
-
- // Initialize the stub
- ((SdevMainProcPtr)ourAddress) ((UInt32)realMainAddress, 0, 0, 0);
- return ourAddress;
- }
-
- void RememberStub (
- Handle stub,
- Handle module)
- {
- UInt32 index;
- for (index = 0; index < sizeof (gGlobals.moduleList) / sizeof (SStubInfo); index++) {
- if (gGlobals.moduleList [index].stub.handle == nil) {
- gGlobals.moduleList [index].stub.handle = stub;
- gGlobals.moduleList [index].module.handle = module;
- gGlobals.moduleList [index].goingUp = false;
- gGlobals.moduleList [index].startingUp = false;
- gGlobals.moduleList [index].amountWider = 0;
- return;
- }
- }
-
- DebugStr ("\pStub list too small");
- }
-
- void RememberFragStub (
- ProcPtr stub,
- ProcPtr module)
- {
- UInt32 index;
- for (index = 0; index < sizeof (gGlobals.moduleList) / sizeof (SStubInfo); index++) {
- if (gGlobals.moduleList [index].stub.procPtr == nil) {
- gGlobals.moduleList [index].stub.procPtr = stub;
- gGlobals.moduleList [index].module.procPtr = module;
- gGlobals.moduleList [index].goingUp = false;
- gGlobals.moduleList [index].startingUp = false;
- gGlobals.moduleList [index].amountWider = 0;
- return;
- }
- }
-
- DebugStr ("\pStub list too small");
- }
-
- pascal OSErr Patched_GetDiskFragment (
- const FSSpec* fileSpec,
- long offset,
- long length,
- Str63 fragName,
- CFragLoadOptions findFlags,
- CFragConnectionID* connID,
- Ptr* mainAddress,
- Str255 errName)
- {
- OSErr result;
- // DebugStr ("\pPatched GetDiskFragment");
-
- result = ((Patched_GetDiskFragmentProcPtr) gMoreCFMPatchesCallThrough)(fileSpec, offset, length, fragName, findFlags,
- connID, mainAddress, errName);
-
- if ((result == noErr) && (*(UInt32*)(&(LMGetCurApName ()[1])) == 'Cont')) {
- *mainAddress = (Ptr)PrepareSdevFragStub ((ProcPtr)*mainAddress);
- }
-
- return result;
- }